package com.contract.spirit.ui.order

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.view.TitleBar
import com.contract.spirit.R
import com.contract.spirit.config.Constant
import com.contract.spirit.databinding.ActivityMyOrderBinding
import com.contract.spirit.func.MyViewPagerAdapter
import com.contract.spirit.manage.CommonConfigManager
import com.contract.spirit.manage.LiveDataEventManager
import com.contract.spirit.ui.order.adapter.NavigatorIndicatorAdapter
import com.contract.spirit.ui.order.fragment.OrderFragment
import com.contract.spirit.ui.order.fragment.OrderGuarantingFrg
import com.contract.spirit.ui.webview.CommonWebViewActivity
import com.jeremyliao.liveeventbus.LiveEventBus
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator

/**
 * @author: zhengjun
 * created: 2021/9/27
 * desc:
 */
class MyOrderActivity : BaseActivity<ActivityMyOrderBinding,OrderViewModel>() {
    var frgList: ArrayList<Fragment> = arrayListOf()
    lateinit var exchangeType:String;
    override fun setContentView(): Int {
        return R.layout.activity_my_order
    }

    override fun initData() {
        exchangeType = intent.getStringExtra(Constant.EXCHANGE_TYPE).toString()
        initTabLayout()
        initTitleBar()
        initVpData()
    }

    private fun initTabLayout() {
        val magicIndicator = mBinding.tabLayout
        val commonNavigator = CommonNavigator(mContext)
        commonNavigator.adapter = NavigatorIndicatorAdapter(mBinding.viewpager)
        commonNavigator.isAdjustMode = true
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mBinding.viewpager)
    }

    private fun initTitleBar() {
        mBinding.titleBar.setTitleButtonOnClickListener(object : TitleBar.TitleButtonOnClickListener{
            override fun LeftButtonOnClick(v: View?) {
                finish()
            }
            override  fun RightButtonOnClick(v: View?) {
                CommonWebViewActivity.goLaunch(
                    mContext,
                    "赔付教程",
                    CommonConfigManager.configInfoResponse.orderPayStudyUrl
                )
            }
        })
    }
    private fun initVpData(){
        frgList.add(OrderGuarantingFrg.newsInstance(exchangeType))
        frgList.add(OrderFragment.newsInstance(GET_INDEMNITY, exchangeType))
        frgList.add(OrderFragment.newsInstance(INDEMNITY_SUCCESS, exchangeType))
        frgList.add(OrderFragment.newsInstance(ALL, exchangeType))
        mBinding.viewpager.adapter = MyViewPagerAdapter(supportFragmentManager,frgList)
        mBinding.viewpager.offscreenPageLimit = frgList.size
    }

    override fun registerNecessaryObserver() {
        LiveEventBus.get<String>(LiveDataEventManager.GAIN_SUCCESS)
            .observe(this,{
                for (i in 1 until frgList.size) {
                    val fragment = frgList[i]
                    if (fragment is OrderFragment) {
                        fragment.getOrderList()
                    }
                }
            })
    }

    companion object{
        fun launch(mContext: Activity, exchangeType:String ){
            val intent = Intent(mContext, MyOrderActivity::class.java)
            intent.putExtra(Constant.EXCHANGE_TYPE, exchangeType)
            mContext.startActivity(intent)
        }
        const val ALL = "0"
        const val GET_INDEMNITY = "1" //获得赔付
        const  val INDEMNITY_SUCCESS = "2" //赔付成功
    }
}